package com.gking.centerSystem.mapper;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gking.centerSystem.entity.Roles;
import com.gking.centerSystem.entity.User;
import com.gking.centerSystem.entity.UserRoles;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserRolesMapper extends BaseMapper<UserRoles> {

    /**
     * 获取用户角色详情,包含默认用户角色
     *
     * @param userId 用户id
     * @return s
     */
    @Select("<script>" +
            "SELECT DISTINCT roles.* " +
            "FROM sy_roles roles " +
            "LEFT JOIN sy_user_roles userRoles ON roles.id = userRoles.roleId AND userRoles.userId = #{userId} " +
            "JOIN sy_root_system rootSystem ON rootSystem.systemCode = roles.systemCode " +
            "WHERE rootSystem.enable = 0 AND rootSystem.isDelete = 0 " +
            "AND roles.isDelete = 0 " +
            "AND (userRoles.userId = #{userId} OR roles.isDefault = 0) " +
            "ORDER BY roles.sortId ASC" +
            "</script>")
    List<Roles> findUsersByCriteria(@Param("userId") String userId);



    /**
     * 获取角色下的用户列表
     *
     * @param roleId   角色id
     * @param account  账号
     * @param nickName 昵称
     * @return l
     */
    @Select("<script>" +
            "SELECT DISTINCT u.*  " +
            "FROM user u " +
            "JOIN sy_user_roles userRoles ON userRoles.userId = u.id " +
            "JOIN sy_roles roles ON roles.id = userRoles.roleId " +
            "WHERE roles.isDelete = 0 AND u.isDelete = 0 " +
            "AND userRoles.roleId = #{roleId} " +
            "<if test='(account != null and account.trim() neq \"\") or (nickName != null and nickName.trim() neq \"\")'>" +
            " AND ( " +
            "   <if test='account != null and account.trim() neq \"\"'>" +
            "     u.account LIKE CONCAT('%', #{account}, '%') " +
            "   </if>" +
            "   <if test='account != null and account.trim() neq \"\" and nickName != null and nickName.trim() neq \"\"'>" +
            "     OR " +
            "   </if>" +
            "   <if test='nickName != null and nickName.trim() neq \"\"'>" +
            "     u.nickName LIKE CONCAT('%', #{nickName}, '%') " +
            "   </if>" +
            " ) " +
            "</if>" +
            "</script>")
    List<User> getUserListByRoleId(@Param("roleId") String roleId,
                                   @Param("account") String account,
                                   @Param("nickName") String nickName);

}
